package com.example.lots_of_demo.utils;

import com.example.lots_of_demo.model.Menu;
import com.example.lots_of_demo.vo.MenuParentVo;
import com.example.lots_of_demo.vo.MenuSubVo;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : LZJ
 * @description : code for LZJ
 * @date : 2021/2/14
 */
public class MenuUtils {

    /**
     * 将菜单实体类 转化成 前端所需要的菜单视图实体对象
     */
    public static List<Object> transferMenuVo(List<Menu> menus){
        List<Object> menuVos = new LinkedList<>();
        menus.forEach(menu -> {
            if(ObjectUtils.isEmpty(menu.getParentCode())){
                MenuParentVo menuParentVo = new MenuParentVo();
                menuParentVo.setIcon(menu.getIcon()).setTitle(menu.getName()).setCode(menu.getCode()).setIndex(menu.getCode());
                List<MenuSubVo> subs = new LinkedList<>();
                menus.forEach(temp -> {
                    if(menu.getCode().equals(temp.getParentCode())){
                        MenuSubVo menuSubVo = new MenuSubVo();
                        menuSubVo.setIcon(temp.getIcon()).setTitle(temp.getName()).setIndex(temp.getIndex()).setCode(temp.getCode());
                        subs.add(menuSubVo);
                    }
                });
                menuParentVo.setSubs(subs);
                menuVos.add(menuParentVo);
            }else if(ObjectUtils.isEmpty(menu.getParentCode()) && !ObjectUtils.isEmpty(menu.getPermissionCode())){
                MenuSubVo menuSubVo = new MenuSubVo();
                menuSubVo.setTitle(menu.getName()).setIcon(menu.getIcon()).setIndex(menu.getIndex());
                menuVos.add(menuSubVo);
            }
        });
        //进行菜单Code排序
        return sortMenuVo(menuVos);
    }

    /**
     * 排序 前端所需要的菜单视图实体对象
     */
    private static List<Object> sortMenuVo(List<Object> menuVos){
        //父菜单排序
        List<Object> result = menuVos.stream().sorted((menuVoOne,menuVoTwo) -> {
            HashMap<String, Object> menuVoOneMap = convertToMap(menuVoOne);
            HashMap<String, Object> menuVoTwoMap = convertToMap(menuVoTwo);
            String menuVoOneCode = (String) menuVoOneMap.get("code");
            String menuVoTwoCode = (String) menuVoTwoMap.get("code");
            return compareMenuVoCode(menuVoOneCode, menuVoTwoCode, MenuSortTypeEnum.PARENT_CODE_SORT);
        }).collect(Collectors.toList());
        //子菜单排序
        for (int i = 0; i < result.size(); i++) {
            Object menuVo = result.get(i);
            HashMap<String, Object> menuVoMap = convertToMap(menuVo);
            if(!ObjectUtils.isEmpty(menuVoMap.get("subs"))){
                MenuParentVo menuParentVo = (MenuParentVo) menuVo;
                List<MenuSubVo> collect = menuParentVo.getSubs().stream()
                        .sorted((menuSubVoOne, menuSubVoTwo) -> compareMenuVoCode(menuSubVoOne.getCode(), menuSubVoTwo.getCode(), MenuSortTypeEnum.SUB_CODE_SORT))
                        .collect(Collectors.toList());
                ((MenuParentVo) menuVo).setSubs(collect);
                result.set(i,menuVo);
            }
        }
        return result;
    }

    /**
     * 根据菜单的Code进行比较大小
     */
    private static int compareMenuVoCode(String menuVoOneCode, String menuVoTwoCode, MenuSortTypeEnum menuSortTypeEnum){
        String menuVoOneSubStr = "";
        String menuVoTwoSubStr = "";
        if(MenuSortTypeEnum.PARENT_CODE_SORT.getCode().equals(menuSortTypeEnum.getCode())){
            menuVoOneSubStr = menuVoOneCode.substring(3, 6);
            menuVoTwoSubStr = menuVoTwoCode.substring(3, 6);
        }else if(MenuSortTypeEnum.SUB_CODE_SORT.getCode().equals(menuSortTypeEnum.getCode())){
            menuVoOneSubStr = menuVoOneCode.substring(6, 9);
            menuVoTwoSubStr = menuVoTwoCode.substring(6, 9);
        }
        return Integer.parseInt(menuVoOneSubStr) - Integer.parseInt(menuVoTwoSubStr);
    }

    /**
     * 将 实体类对象 转化成 Map
     */
    public static HashMap<String, Object> convertToMap(Object obj){
        HashMap<String, Object> map = null;
        try {
            map = new HashMap<String, Object>();
            Field[] fields = obj.getClass().getDeclaredFields();
            for (int i = 0, len = fields.length; i < len; i++) {
                String varName = fields[i].getName();
                boolean accessFlag = fields[i].isAccessible();
                fields[i].setAccessible(true);

                Object o = fields[i].get(obj);
                if (o != null) {
                    map.put(varName, o.toString());
                }

                fields[i].setAccessible(accessFlag);
            }
        } catch (IllegalAccessException e) {
            return new HashMap<>(1);
        }

        return map;
    }

    /**
     * 菜单排序类型
     */
    @AllArgsConstructor
    enum MenuSortTypeEnum{

        /**
         * 菜单父节点排序
         */
        PARENT_CODE_SORT("Parent","菜单父节点排序"),
        /**
         * 菜单子节点排序
         */
        SUB_CODE_SORT("Sub", "菜单子节点排序");

        @Getter
        @Setter
        private String code;
        @Getter
        @Setter
        private String desc;

    }

}
